package com.show.web.event;

import com.show.util.FileUtil;
import com.show.util.HelperClazz;
import com.show.util.PageUtil;
import com.show.dto.Event;
import com.show.dto.grid.Grid;
import com.show.dto.sys_emp.SysEmp;
import com.show.dto.sys_emp.SysEmpRole;
import com.show.dto.sys_menu.SysMenu;
import com.show.dto.sys_org.SysOrg;
import com.show.filter.LoginController;
import com.show.service.emp.EmpService;
import com.show.service.event.EventService;
import com.show.service.grid.GridService;
import com.show.service.org.SysOrgService;
import com.show.service.role.RoleService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 任务处理跳转控制器
 * Created by 王硕 on 2018/1/30.
 */
@Controller
@RequestMapping(value = "/eventTurn")
public class EventTurnController {

    @Autowired
    private EmpService empService;
    @Autowired
    private EventService eventService;
    @Autowired
    private LoginController loginController;
    @Autowired
    private SysOrgService sysOrgService;
    @Autowired
    private GridService gridService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private FileUtil fileUtil;

    /**
     * 组任务Index
     *
     * @return
     */
    @GetMapping("/toWorkIndex")
    public String toWorkIndex(HttpServletRequest request, Map<String, Object> map, String loginName) {
        Map<String, Cookie> cookieMap = HelperClazz.ReadCookieMap(request);
        Cookie cookie = (Cookie) cookieMap.get("userName");
        String userName = cookie.getValue().replace("'", "");
        List<SysMenu> list = loginController.getMenus(userName);
        SysEmp emp = empService.getEmpByLogin(userName);
        map.put("emp", emp);
        map.put("list", list);
        map.put("empName", userName);
        return "event/eventIndex";
    }

    /**
     * 组任务List
     *
     * @param map
     * @param page
     * @param pageSize
     * @param loginName
     * @return
     */
    @GetMapping("/toZWorkList")
    public String toZWorkList(HttpServletRequest request, Map<String, Object> map, Integer page, Integer pageSize, String loginName) {
        Map<String, Cookie> cookieMap = HelperClazz.ReadCookieMap(request);
        Cookie cookie = (Cookie) cookieMap.get("userName");
        String userName = cookie.getValue().replace("'", "");
        List<SysMenu> list = loginController.getMenus(userName);
        SysEmp emp = empService.getEmpByLogin(userName);
        map.put("emp", emp);
        map.put("list", list);
        map.put("empName", userName);
        //工作流信息list
        PageUtil pageUtil = eventService.queryEventByIds(emp.getEmpId(), page, pageSize);
        map.put("datas", pageUtil);
        map.put("SERVERIP", fileUtil.getSERVERIP());
        map.put("LNG", fileUtil.getLNG());
        map.put("LAT", fileUtil.getLAT());
        return "event/eventZList";
    }


    /**
     * 个人任务Index
     *
     * @return
     */
    @GetMapping("/toPersonalTaskIndex")
    public String toPersonalTaskIndex(HttpServletRequest request, Map<String, Object> map, String eventId) {
        /**/
//        SysEmp emp = empService.getEmpByLogin(loginName);
//        map.put("emp", emp);//emp信息
        /**/
        Map<String, Cookie> cookieMap = HelperClazz.ReadCookieMap(request);
        Cookie cookie = (Cookie) cookieMap.get("userName");
        String userName = cookie.getValue().replace("'", "");
        SysEmp emp = empService.getEmpByLogin(userName);
        List<SysMenu> list = loginController.getMenus(emp.getLoginName());
        map.put("emp", emp);
        map.put("list", list);
        map.put("empName", userName);
        map.put("eventId", eventId);
        return "event/toPersonalTaskIndex";
    }

    /**
     * 个人任务List
     *
     * @param map
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/toGWorkList")
    public String toGWorkList(HttpServletRequest request, Map<String, Object> map, Integer page, Integer pageSize, String eventId) {
        map.put("eventId", eventId);
        /* 获取当前登陆人名字 */
        Map<String, Cookie> cookieMap = HelperClazz.ReadCookieMap(request);
        Cookie cookie = (Cookie) cookieMap.get("userName");
        String userName = cookie.getValue().replace("'", "");
        map.put("empName", userName);
        /* 获取当前登陆人信息 */
        SysEmp emp = empService.getEmpByLogin(userName);
        map.put("emp", emp);
        /* 获取角色拼串 */
        String roleIdss = empService.findEmpRoleIds(emp.getEmpId());
        /* 菜单列表 */
        List<SysMenu> list = loginController.getMenus(emp.getLoginName());
        map.put("list", list);
        if (roleIdss.indexOf("18") != -1) {
            /* 查询可下派当前登陆人社区下的执法人员 */
            List<SysEmp> sysEmpList = this.LawEnforcementForTree(emp.getEmpId().toString());
            if (sysEmpList != null) { map.put("empList", sysEmpList); }
        }
        if (roleIdss.indexOf("17") != -1) {
            /* 根据当前登陆人ID获取当前登陆人所在街道（部门）可下派的社区 */
            List<SysOrg> sysOrgList1 = sysOrgService.ChooseTheCommunity(empService.findOrgByEmpId(emp.getEmpId().toString()).get(0).getOrgId());
            if (sysOrgList1 != null) { map.put("orgList", sysOrgList1); }
        }
        /* 查询可上报的街道集合 */
        List<SysOrg> sysOrgList = sysOrgService.ChooseTheStreetForTree();
        if (sysOrgList != null) { map.put("pOrgList", sysOrgList); }
        /* 分页list */
        PageUtil pageUtil = eventService.taskListByUserId(String.valueOf(emp.getEmpId()), page, pageSize);
        map.put("datas", pageUtil);
        map.put("SERVERIP", fileUtil.getSERVERIP());
        map.put("LNG", fileUtil.getLNG());
        map.put("LAT", fileUtil.getLAT());
        return "event/eventGList";
    }

    /**
     * 查询可下派当前登陆人社区下的执法人员
     *
     * @param empId
     * @return
     */
    public List<SysEmp> LawEnforcementForTree(String empId) {
        //获取当前登陆人所在的部门信息
        String ordids = sysOrgService.getOrgIdByEmpId(empId);
        //获取当前登陆人所在的社区部门
        List<SysOrg> list = sysOrgService.findOrgByOrgIds(ordids);
        //社区部门ID集合
        String SQirgIds = "";
        for (SysOrg sysOrg : list) {
            SQirgIds += sysOrg.getOrgId() + ",";
        }
        SQirgIds = SQirgIds.substring(0, SQirgIds.length() - 1);
        //获取所在社区下并且拥有执法人员角色的人
        List<SysEmp> emps = eventService.LawEnforcementForTree(SQirgIds);
        return emps;
    }

    /**
     * 个人任务List
     *
     * @param map
     * @param eventId
     * @return
     * @Auther yxd
     */
    @GetMapping("/toGWorkListByEventId")
    @ApiImplicitParams({@ApiImplicitParam(name = "eventId", paramType = "query", value = "事件ID", required = true, dataType = "String")
    })
    public String toGWorkListByEventId(HttpServletRequest request, Map<String, Object> map, String eventId) {
        Map<String, Cookie> cookieMap = HelperClazz.ReadCookieMap(request);
        Cookie cookie = (Cookie) cookieMap.get("userName");
        String userName = cookie.getValue().replace("'", "");
        List<SysMenu> list = loginController.getMenus(userName);
        SysEmp emp = empService.getEmpByLogin(userName);
        map.put("emp", emp);
        map.put("list", list);
        map.put("empName", userName);
        map.put("eventId", eventId);
        return "event/eventIndex";
    }

//    /**
//     * @author 姜文
//     * @Param
//     * @date 2018年1月29日, PM 02:26:53
//     */
//    @GetMapping("/toHotlineIndex")
//    public String toHotlineIndex() {
//        return "hotline_accept/hotline_index";
//    }
//
//    /**
//     * @author 姜文
//     * @Param
//     * @date 2018年1月29日, PM 02:26:53
//     */
//    @GetMapping("/toHotLineInsert")
//    public String toHotLineInsert() {
//        return "hotline_accept/hotline_list";
//    }

    /**
     * 上报事件 跳转index
     *
     * @author 姜文
     * @Param
     * @date 2018年3月6日, AM 09:35:51
     */
    @GetMapping("/toEventIndex")
    public String toEventIndex(HttpServletRequest request, Map<String, Object> map) {
        Map<String, Cookie> cookieMap = HelperClazz.ReadCookieMap(request);
        Cookie cookie = (Cookie) cookieMap.get("userName");
        String userName = cookie.getValue().replace("'", "");
        SysEmp emp = empService.getEmpByLogin(userName);
        List<SysMenu> list = loginController.getMenus(emp.getLoginName());
        map.put("emp", emp);//emp信息
        map.put("list", list);
        map.put("empName", userName);
        return "monitor/monitor_index";
    }

    /**
     * 上报事件 跳转list
     *
     * @author 姜文
     * @Param
     * @date 2018年3月6日, AM 09:35:56
     */
    @GetMapping("/toEventList")
    public String toEventList(HttpServletRequest request, Map<String, Object> map) {
        Map<String, Cookie> cookieMap = HelperClazz.ReadCookieMap(request);
        Cookie cookie = (Cookie) cookieMap.get("userName");
        String userName = cookie.getValue().replace("'", "");
        SysEmp emp = empService.getEmpByLogin(userName);
        List<SysMenu> list = loginController.getMenus(emp.getLoginName());
        map.put("emp", emp);
        map.put("list", list);
        map.put("empName", userName);
        map.put("SERVERIP", fileUtil.getSERVERIP());
        map.put("LNG", fileUtil.getLNG());
        map.put("LAT", fileUtil.getLAT());
        return "monitor/monitor_list";
    }

    /**
     * 事件列表 跳转index
     *
     * @author 姜文
     * @Param
     * @date 2018年4月2日, AM 09:22:59
     */
    @GetMapping("/queryEventForIndex")
    public String queryEventForIndex(HttpServletRequest request, Map<String, Object> map) {
        Map<String, Cookie> cookieMap = HelperClazz.ReadCookieMap(request);
        Cookie cookie = (Cookie) cookieMap.get("userName");
        String userName = cookie.getValue().replace("'", "");
        SysEmp emp = empService.getEmpByLogin(userName);
        List<SysMenu> list = loginController.getMenus(emp.getLoginName());
        map.put("emp", emp);
        map.put("list", list);
        map.put("empName", userName);
        return "Event_list/Event_index";
    }

    /**
     * 事件列表 跳转list
     *
     * @author 姜文
     * @Param
     * @date 2018年4月2日, AM 09:22:59
     */
    @GetMapping("/queryEventForList")
    public String queryEventForList(HttpServletRequest request, Map<String, Object> map, Event event, Integer page, Integer pageSize) {
        if(event != null) { map.put("event", event); }
        Map<String, Cookie> cookieMap = HelperClazz.ReadCookieMap(request);
        Cookie cookie = (Cookie) cookieMap.get("userName");
        String userName = cookie.getValue().replace("'", "");
        map.put("empName", userName);
        SysEmp emp = empService.getEmpByLogin(userName);
        event.setEmpId(emp.getEmpId());
        map.put("emp", emp);
        List<SysMenu> list = loginController.getMenus(emp.getLoginName());
        map.put("list", list);
        List<SysEmpRole> sysEmpRoleList = roleService.findEmpRoleByEmpId(emp.getEmpId().toString());
        for(SysEmpRole ser:sysEmpRoleList){
            if("19".equals(ser.getRoleId())) event.setQueryType(1);
            if("18".equals(ser.getRoleId())||"21".equals(ser.getRoleId())) event.setQueryType(2);
            if("17".equals(ser.getRoleId())||"23".equals(ser.getRoleId())) event.setQueryType(3);
            if("16".equals(ser.getRoleId())) event.setQueryType(4);
        }
        PageUtil<Event> pageUtil = eventService.findAllEventForPage(event, page, pageSize);
        for (Event ev : pageUtil.getContent()) {
            if (ev.getEmpId() != null) {
                ev.setEmpName(empService.findOne(ev.getEmpId()).getEmpName());
            } else {
                ev.setEmpName("无法获取");
            }
            ev.setVal(eventService.selectValFromKv(ev.getEventSource().toString()).getVal());
            ev.setEventHandleType(eventService.selectEventHandleType(ev.getEventId().toString()));
        }
        map.put("datas", pageUtil);
        return "Event_list/event_list";
    }

    /**
     * 事件列表 查看详情
     *
     * @author 姜文
     * @Param
     * @date 2018年4月2日, AM 09:22:59
     */
    @GetMapping("/queryEventFindOne/{eventId}")
    public String queryEventFindOne(Map<String, Object> map, @PathVariable("eventId") String eventId){
        map.put("eventId", eventId);
        Event event = eventService.findOneForTypeId(eventId);
        if(event.getGridId() == null){
            event.setGridId(0);
        }
        Grid cgGrid = gridService.findOneById(event.getGridId());
        if(cgGrid != null){
            event.setGridName(cgGrid.getGridName());
        }else{
            event.setGridName(null);
        }
        SysEmp emp = empService.findOne(event.getEmpId());
        event.setEmpName(emp.getEmpName());
        event.setVal(eventService.selectValFromKv(event.getEventTypeId().toString()).getVal());
        map.put("event", event);
        map.put("SERVERIP", fileUtil.getSERVERIP());
        map.put("LNG", fileUtil.getLNG());
        map.put("LAT", fileUtil.getLAT());
        return "Event_list/event_see";
    }
}
